package com.example.spring_mysql.excel

import org.apache.poi.ss.formula.functions.T
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.io.InputStreamResource
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile
import java.nio.file.Paths


@RestController
class TranslationController {

    @Autowired
    lateinit var translationService: TranslationService

    @PostMapping("/api/translate/upload")
    fun handleUpload(@RequestParam("file") file: MultipartFile): ResponseEntity<ByteArray> {
        try {
            println(file.originalFilename)
            val parsedData: Map<String, List<String>> = translationService!!.parse(file)
            val zipData = translationService.buildZip(parsedData)
            return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=android_strings.zip")
                .contentType(MediaType.APPLICATION_OCTET_STREAM).body(zipData)

        } catch (e: Exception) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(("Error: " + e.message).toByteArray(Charsets.UTF_8))
        }
    }
}
